package Model;

import java.util.ArrayList;

import Core.Board;
import Core.Game;
import Core.Player;
import Core.Position;
import Core.Symbol;

public class Puissance4 extends Game
{
    public Puissance4(Player player1, Player player2, Board board)
    {
	super(player1, player2, board);
	this.alignement = 4;
    }

    private int valueForSymbol(Symbol s)
    {
	if (s.equals(Puissance4Symbole.CIRCLER))
	{
	    return +1;
	} else if (s.equals(Puissance4Symbole.CIRCLEY))
	{
	    return -1;
	}
	return 0;
    }

    public void play()
    {
	this.currentPlayer = player2;
	this.board.display();
	while (!gameIsFinished())
	{

	    Position position = this.currentPlayer.play(this.validMoves(board),
		    this.board);

	    if (board.isValid(position) && board.isFree(position))
	    {
		board.set(position, currentPlayer.getSymbole());
		this.board.display();
		this.setChanged();
		this.notifyObservers(position);
		if (this.currentPlayer == this.player1)
		{
		    this.currentPlayer = this.player2;
		} else
		{
		    this.currentPlayer = this.player1;
		}
	    }
	}

    }



    @Override
    public boolean gameIsFinished()
    {
	if (this.alignmentFound(this.alignement) || this.gridIsFilled())
	    return true;
	return false;
    }

    @Override
    public boolean gameIsDraw()
    {
	if (this.gridIsFilled() && !this.alignmentFound(this.alignement))
	    return true;
	return false;
    }

    @Override
    public ArrayList<Position> validMoves(Board board)
    {
	ArrayList<Position> validMoves = new ArrayList<Position>();
	for (int i = 0; i < this.board.getLines(); i++)
	{
	    for (int j = 0; j < this.board.getColumns(); j++)
	    {
		//La ligne du bas est toujours valide si il n'y a personne
		if (i == 5)
		{
		    if (board.isValid(new Position(j, i))
			    && Puissance4Symbole.NONE.equals(board
				    .getSymbol(new Position(j, i))))
		    {

			validMoves.add(new Position(j, i));

		    }
		}
		// Les autres cases sont valides si il n'y a personne et qu'il y a quelqu'un "en dessous"
		else
		{
		    if (board.isValid(new Position(j, i))
			    && Puissance4Symbole.NONE.equals(board
				    .getSymbol(new Position(j, i)))
			    && !Puissance4Symbole.NONE.equals(board
				    .getSymbol(new Position(j, i + 1))))
		    {
		
		    }
		}
	    }
	}

	return validMoves;
    }

    @Override
    public boolean alignmentFound(int alignement)
    {
	// TODO Auto-generated method stub
	return false;
    }

}
